<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script src="https://unpkg.com/vue@next"></script>
<div id="app">
    <form v-on:submit.prevent="addNewTodo">
        <label for="new-todo">Add a todo</label>
        <input v-model="newTodoText" id="new-todo" placeholder="E.g. Feed the cat"/>
        <button>Add</button>
    </form>

    <ul>
        <todo-item v-for="(todo, index) in todos" :key="todo.id" :title="todo.title" @remove="todos.splice(index, 1)">

        </todo-item>
    </ul>
</div>
<script>
    const app = Vue.createApp({
        data(){
            return {
                newTodoText: '',
                todos: [
                    {
                        id: 1,
                        title: 'Do the dishes'
                    },
                    {
                        id: 2,
                        title: 'Take out the trash',
                    },
                    {
                        id: 3,
                        title: 'Mow the lawn'
                    }
                ],
                nextTodoId: 4
            }
        },
        methods:{
            addNewTodo(){
                this.todos.push({
                    id: this.nextTodoId++,
                    title: this.newTodoText
                });
                this.newTodoText = '';
            }
        }
    });
    app.component('todo-item', {
        template: `<li>{{title}}<button @click="$emit('remove')">Remove</button></li>`,
        // 通过props将title属性传递给组件
        props:['title']
    });
    app.mount('#app');
</script>
</body>
</html>
